<template>
  <div id="login">
    <div class="login_box">
      <div class="avater">
        <img src="../assets/image/logo.png" alt="登录头像" />
      </div>
      <!-- 登录表单区 -->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="formRules"
        label-width="0px"
        class="login_form"
      >
        <!--用户名-->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-yonghu"></el-input>
        </el-form-item>
        <!--密码-->
        <el-form-item prop="password">
          <el-input type="password" v-model="loginForm.password" prefix-icon="iconfont icon-mima"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="infor" @click="resetLogin">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  name: "login",
  created() {},
  data() {
    return {
      auth: null,
      // 登录表单的绑定数据
      loginForm: {
        username: "",
        password: ""
      },
      // 定义表单验证规则
      formRules: {
        // 验证用户名是否合法
        username: [
          { required: true, message: "请输入登录名称", trigger: "blur" },
          { min: 1, max: 5, message: "长度在 1 到 5 个字符", trigger: "blur" }
        ],
        // 验证密码是否合法
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      }
    };
  },
  methods: {
    // 点击重置按钮重置登录表单
    resetLogin() {
      this.$refs.loginFormRef.resetFields();
    },
    login() {
      this.$refs.loginFormRef.validate(async valid => {
        // 如果表单验证不通过直接停止运行
        if (!valid) return;
        // 先匿名登录
        let anonymousLogin = await this.$login();
        // 若匿名登录失败就直接停止了
        if (!anonymousLogin) return;
        let loginres = await this.$cloudbase.callFunction({
          name: "login",
          data: this.loginForm
        });
        if (loginres.result.data.length == 0) {
          return this.$message.error(`登录失败`);
        }
        this.$message.success(`登录成功了`);
        // 登录成功后保存，保持登录状态吧
        window.sessionStorage.setItem("token", anonymousLogin);
        this.$router.push("/home");
      });
    }
  }
};
</script>
<style lang="less" scoped>
#login {
  width: 100vw;
  height: 100vh;
  background-color: #2b4b6b;
}

.login_box {
  width: 500px;
  height: 350px;
  background: #ffffff;
  border-radius: 6px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  &:hover {
    cursor: pointer;
  }
}

// 登录头像了
.avater {
  -webkit-user-select: none;
  user-select: none;
  width: 130px;
  height: 130px;
  border: 1px solid #eeeeee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #dddddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #ffffff;
  > img {
    -webkit-user-select: none;
    user-select: none;
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eeeeee;
    text-align: center;
  }
}
// 登录表单
.login_form {
  position: absolute;
  bottom: 30px;
  width: 100%;
  box-sizing: border-box;
  padding: 0 20px;
}
// 登录的按钮
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>